package com.zunipe.config;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.zunipe.OreSim.Ore;
import com.zunipe.ZunipeTools;
import com.zunipe.until.Util;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.JsonHelper;

import java.io.*;
import java.nio.file.Path;
import java.util.Iterator;

public class ZunipeConfig
{
    public static final Path PATH = FabricLoader.getInstance().getConfigDir();

    public static boolean createOptionConfig()
    {
        File file = PATH.resolve(ZunipeTools.MODID + ".json").toFile();
        if (!file.exists())
        {
            try
            {
                file.createNewFile();
                saveOptionConfig();
            } catch (IOException e)
            {
                return false;
            }
        }
        return true;
    }

    public static boolean loadOptionConfig()
    {
        File file = PATH.resolve(ZunipeTools.MODID + ".json").toFile();
        if (!file.exists())
            createOptionConfig();
        StringBuilder s = new StringBuilder();
        try
        {
            BufferedReader bufferedReader2 = new BufferedReader(new FileReader(file));
            String tempString;
            while ((tempString = bufferedReader2.readLine()) != null)
            {
                s.append(tempString);
            }
        } catch (IOException e)
        {
            ZunipeTools.LOGGER.error(e.getMessage());
            return false;
        }

        try
        {
            JsonParser jsonParser = new JsonParser();
            JsonObject jsonObject = jsonParser.parse(s.toString()).getAsJsonObject();
            Util.xrayRange = JsonHelper.getInt(jsonObject, "XrayScanningRange");
            Util.xrayScanningInterval = JsonHelper.getInt(jsonObject, "XrayScanningInterval");
            Util.clickLeftInterval = JsonHelper.getInt(jsonObject, "ClickLeftInterval");
            Util.clickRightInterval = JsonHelper.getInt(jsonObject, "ClickRightInterval");
            Util.yawScope = JsonHelper.getInt(jsonObject, "YawScope");
            Util.pitchScope = JsonHelper.getInt(jsonObject, "PitchScope");
            Util.tick = JsonHelper.getInt(jsonObject, "AutoAttackTick");
            Util.detectDistance = JsonHelper.getInt(jsonObject, "DetectDistance");
            Util.yawScope = JsonHelper.getInt(jsonObject, "YawScope");
            Util.autoRedPacket = JsonHelper.getBoolean(jsonObject, "AutoRedPacket");

            Ore.diamond.setValue(JsonHelper.getBoolean(jsonObject, "diamond"));
            Ore.emerald.setValue(JsonHelper.getBoolean(jsonObject, "emerald"));
            Ore.gold.setValue(JsonHelper.getBoolean(jsonObject, "gold"));
            Ore.iron.setValue(JsonHelper.getBoolean(jsonObject, "iron"));
            Ore.copper.setValue(JsonHelper.getBoolean(jsonObject, "copper"));
            Ore.redstone.setValue(JsonHelper.getBoolean(jsonObject, "redstone"));
            Ore.lapis.setValue(JsonHelper.getBoolean(jsonObject, "lapis"));
            Ore.coal.setValue(JsonHelper.getBoolean(jsonObject, "coal"));
            Ore.debris.setValue(JsonHelper.getBoolean(jsonObject, "debris"));

        } catch (Exception e)
        {
            ZunipeTools.LOGGER.error(e.getMessage());
            try
            {
                saveOptionConfig();
            } catch (IOException ex)
            {
                ZunipeTools.LOGGER.error(ex.getMessage());
            }
        }
        return true;
    }

    public static void saveOptionConfig() throws IOException
    {
        File file = PATH.resolve(ZunipeTools.MODID + ".json").toFile();
        if (!file.exists())
            createOptionConfig();
        JsonObject json = optionToJson();
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file.getPath())));
        pw.print(json);
        pw.flush();
        pw.close();
    }

    public static JsonObject optionToJson()
    {
        JsonObject json = new JsonObject();
        json.addProperty("XrayScanningRange", Util.xrayRange);
        json.addProperty("XrayScanningInterval", Util.xrayScanningInterval);
        json.addProperty("ClickLeftInterval", Util.clickLeftInterval);
        json.addProperty("ClickRightInterval", Util.clickRightInterval);
        json.addProperty("YawScope", Util.yawScope);
        json.addProperty("PitchScope", Util.pitchScope);
        json.addProperty("AutoAttackTick", Util.tick);
        json.addProperty("DetectDistance", Util.detectDistance);
        json.addProperty("YawScope", Util.yawScope);
        json.addProperty("AutoRedPacket", Util.autoRedPacket);
        json.addProperty("diamond", Ore.diamond.getValue());
        json.addProperty("emerald", Ore.emerald.getValue());
        json.addProperty("gold", Ore.gold.getValue());
        json.addProperty("iron", Ore.iron.getValue());
        json.addProperty("copper", Ore.copper.getValue());
        json.addProperty("lapis", Ore.lapis.getValue());
        json.addProperty("redstone", Ore.redstone.getValue());
        json.addProperty("coal", Ore.coal.getValue());
        json.addProperty("debris", Ore.debris.getValue());
        return json;
    }

    public static boolean createConfig()
    {
        File f = PATH.resolve(ZunipeTools.MODID + "_itemlist.txt").toFile();
        File f2 = PATH.resolve(ZunipeTools.MODID + "_exclusionitemlist.txt").toFile();
        if (!f.exists())
        {
            try
            {
                f.createNewFile();
            } catch (IOException e)
            {
                return false;
            }
            try
            {
                PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f.getPath())));
                for (Iterator<String> it = Util.itemList.stream().iterator(); it.hasNext(); )
                {
                    String name = it.next();
                    pw.println(name);
                }
                pw.flush();
                pw.close();
            } catch (IOException e)
            {
                return false;
            }
        }
        if (!f2.exists())
        {
            try
            {
                f2.createNewFile();
                PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f2.getPath())));
                pw.println("无");
                pw.flush();
                pw.close();
            } catch (IOException e)
            {
                return false;
            }
        }
        return true;
    }

    public static void loadConfig()
    {
        File f = PATH.resolve(ZunipeTools.MODID + "_itemlist.txt").toFile();
        File f2 = PATH.resolve(ZunipeTools.MODID + "_exclusionitemlist.txt").toFile();
        if (!f.exists() || !f2.exists())
            createConfig();
        try
        {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(f));
            String tempString;
            Util.itemList.clear();
            while ((tempString = bufferedReader.readLine()) != null)
            {
                Util.itemList.add(tempString);
            }
            bufferedReader.close();

            BufferedReader bufferedReader2 = new BufferedReader(new FileReader(f2));
            Util.exclusionItemList.clear();
            while ((tempString = bufferedReader2.readLine()) != null)
            {
                Util.exclusionItemList.add(tempString);
            }
            bufferedReader2.close();
        } catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }
}
